package com.chen.androidstudy.activity;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.CallLog;
import android.provider.MediaStore;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.base.library.permission.PermissionUtils;
import com.chen.androidstudy.R;
import com.permissionx.guolindev.callback.RequestCallback;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: CHong
 * @CreateDate: 2024/9/5 0005
 * @Description:
 */

public class SimCallActivity extends AppCompatActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sim_call);
//        getSim();
        List<String> list = new ArrayList<>();
        list.add(Manifest.permission.CALL_PHONE);
        list.add(Manifest.permission.READ_PHONE_STATE);
        list.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
        list.add(Manifest.permission.READ_EXTERNAL_STORAGE);
        PermissionUtils.INSTANCE.requestPermission(this, list, new RequestCallback() {
            @Override
            public void onResult(boolean allGranted, @NonNull List<String> grantedList, @NonNull List<String> deniedList) {
                if (allGranted) {
                    Log.e("chen", "permission ok");
                    getSim();
                    getAudio();
                }
            }
        });
        findViewById(R.id.txtCall).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                call("tel:" + "10086", 1);
            }
        });
        findViewById(R.id.txtCall2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                call("tel:" + "10086", 2);
            }
        });
    }

    private void getSim() {
        // 获取TelephonyManager对象
        TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
// 获取SIM卡数量
        int simCount = telephonyManager.getPhoneCount();
        Log.e("chen", "simCount:" + simCount);
    }

    //指定SIM卡拨打
    public static final String[] dualSimTypes = {"subscription", "Subscription",
            "com.android.phone.extra.slot",
            "phone", "com.android.phone.DialingMode",
            "simId", "simnum", "phone_type",
            "simSlot"};

    //申请到权限后打电话
    public void call(String telPhone, int type) {
        if (PermissionUtils.INSTANCE.hasPermission(Manifest.permission.CALL_PHONE)) {
            Intent callIntent = new Intent(Intent.ACTION_CALL).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            callIntent.setData(Uri.parse(telPhone));
            for (int i = 0; i < dualSimTypes.length; i++) {
                //0代表卡1,1代表卡2
                callIntent.putExtra(dualSimTypes[i], type);
            }
            this.startActivity(callIntent);
        }
    }

    public void getAudio(){
//        Uri queryUri = CallLog.Calls.CONTENT_URI;
        Uri queryUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
//        String selection = CallLog.Calls.TYPE + " = ? AND " + CallLog.Calls.ID + " = ?";
//        String[] selectionArgs = new String[]{String.valueOf(CallLog.Calls.VOICEMAIL_TYPE), String.valueOf(callId)};

        Cursor cursor = getContentResolver().query(
                queryUri,
                null,
                null,
                null,
                null
        );
        if (cursor != null){
            while (cursor.moveToNext()){
                String data = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA));
                Log.e("chen","data:" + data);
            }
            cursor.close();
        }
    }
}
